package com.dfec.book.controller;

import com.dfec.book.entity.BookEntity;
import com.dfec.book.service.BookService;
import com.dfec.book.util.AjaxResult;
import com.dfec.book.util.PageUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 图书管理
 * @author tangrg
 * @email 1446232546@qq.com
 * @date 2025-11-19 11:39:13
 */
@RestController
@RequestMapping("/book")
@Api(tags = "图书管理")
@ApiSort(2)
public class BookController {

    @Resource
    private BookService bookService;

    /**
     * 分页查询图书
     */
    @PostMapping("/page")
    @ApiOperation("分页查询图书")
    public AjaxResult<PageUtils> getBooksByPage(
            @ApiParam(value = "图书查询参数", required = true)
            @RequestBody BookEntity bookEntity) {

        PageUtils page = bookService.getBooksByPage(bookEntity);
        return AjaxResult.ok(page);
    }

    /**
     * 添加图书
     */
    @PostMapping("/add")
    @ApiOperation("添加图书")
    public AjaxResult<Boolean> addBook(
            @ApiParam(value = "图书实体", required = true)
            @RequestBody BookEntity book) {

        try {
            boolean result = bookService.addBook(book);
            return result ? AjaxResult.ok() : AjaxResult.error("添加失败");
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 更新图书
     */
    @PostMapping("/update")
    @ApiOperation("更新图书")
    public AjaxResult<Boolean> updateBook(
            @ApiParam(value = "图书实体", required = true)
            @RequestBody BookEntity book) {

        try {
            boolean result = bookService.updateBook(book);
            return result ? AjaxResult.ok() : AjaxResult.error("更新失败");
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除图书
     */
    @PostMapping("/delete")
    @ApiOperation("删除图书")
    public AjaxResult<Boolean> deleteBook(
            @ApiParam(value = "图书ID", required = true)
            @RequestBody List<Integer> bookIds) {

        try {
            boolean result = bookService.deleteBook(bookIds);
            return result ? AjaxResult.ok() : AjaxResult.error("删除失败");
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据ID获取图书
     */
    @GetMapping("/getById")
    @ApiOperation("根据ID获取图书")
    public AjaxResult<BookEntity> getBookById(
            @ApiParam(value = "图书ID", required = true)
            @RequestParam Integer bookId) {

        BookEntity book = bookService.getBookById(bookId);
        return AjaxResult.ok(book);
    }



    /**
     * 更新图书借阅状态
     */
    @PostMapping("/updateBorrowStatus")
    @ApiOperation("更新图书借阅状态")
    public AjaxResult<Boolean> updateBookBorrowStatus(
            @ApiParam(value = "图书ID", required = true)
            @RequestParam Integer bookId,
            @ApiParam(value = "借阅状态", required = true, example = "true")
            @RequestParam boolean isBorrowed) {
        try {
            boolean result = bookService.updateBookBorrowStatus(bookId, isBorrowed);
            return result ? AjaxResult.ok() : AjaxResult.error("状态更新失败");
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }


}